package is.Hazir.SatGPS;

import android.database.Cursor;
import android.test.AndroidTestCase;

public class TestDataProvider extends AndroidTestCase {
	HazirDataProvider database;
	long place_1_id, place_2_id;

	protected void setUp() throws Exception {
		super.setUp();
		database = new HazirDataProvider(getContext());
		Place place = new Place();
		place.latitude = 0;
		place.longitude = 0;
		place.name = "Stadur 1";
		place.radius = 30;
		place.silent = true;
		place.sms = true;
		place.smsText = "Texti SMS 1";
		place_1_id = database.store_place(place);
		
		Place place2 = new Place();
		place2.latitude = 90;
		place2.longitude = 90;
		place2.name = "Stadur 2";
		place2.radius = 30;
		place2.silent = true;
		place2.sms = true;
		place2.smsText = "Texti SMS 2";
		place_2_id = database.store_place(place2);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		database.delete_place(place_1_id);
		database.delete_place(place_2_id);
		database.close();
	}

	public final void testGet_placeIntInt() {
		Place place1 = database.get_place(0, 0);
		assertEquals(place1.id, place_1_id);
	}

	public final void testGet_placeLong() {
		Place place2 = database.get_place(place_2_id);
		assertEquals(place2.name, "Stadur 2");
	}

	public final void testGet_placeString() {
		Place place2 = database.get_place("Stadur 2");
		assertEquals(place2.id, place_2_id);
	}

	public final void testAll_places() {
		Cursor cursor = database.all_places();
		assertTrue(cursor.getCount() >= 2);
		boolean place_1_found = false;
		boolean place_2_found = false;
		while (cursor.moveToNext()) {
			if (cursor.getLong(HazirDataProvider.PLACES_COL_NUM_ID) == place_1_id) {
				place_1_found = true;
			}
			if (cursor.getLong(HazirDataProvider.PLACES_COL_NUM_ID) == place_2_id) {
				place_2_found = true;
			}
		}
		assertTrue(place_1_found);
		assertTrue(place_2_found);
		cursor.close();
	}

	public final void testStore_place() {
		assertNotNull(place_1_id);
		assertNotNull(place_2_id);
		Place place1 = database.get_place(place_1_id);
		place1.name = "Stadur 1 - breyttur";
		database.store_place(place1);
		Place stored_place = database.get_place(place_1_id);
		assertEquals(stored_place.name, "Stadur 1 - breyttur");
		assertEquals(stored_place.silent, true);
		assertEquals(stored_place.smsText, "Texti SMS 1");
	}

	public final void testDelete_place() {
		Place place2_copy = database.get_place(place_2_id);
		place2_copy.id = 0;
		long place2_copy_id = database.store_place(place2_copy);
		database.delete_place(place2_copy_id);
		assertNull(database.get_place(place2_copy_id));
	}

}
